/**
 * 作为系统管理员和师傅管理员的调用的权限的js文件
 * **/
import { constantRouterMap, doctorRouterMap, systemRouterMap} from "@/router";
import { getUserPermission} from "@/api/permission";
import store from '@/store'
import Cookie from 'js-cookie'
import {tips} from "@/common/js/optionTips";

//判断是否有权限访问该菜单
function hasPermission(menus, route) {
  if (route.name) {
    let currMenu = getMenu(route.name, menus);
    if (currMenu!=null) {
      //设置菜单的标题、图标和可见性
      if (currMenu.title != null && currMenu.title !== '') {
        route.meta.title = currMenu.title;
      }
      if (currMenu.icon != null && currMenu.title !== '') {
        route.meta.icon = currMenu.icon;
      }
      if(currMenu.hidden!=null){
        route.hidden = currMenu.hidden !== 0;
      }
      if (currMenu.sort != null && currMenu.sort !== '') {
        route.sort = currMenu.sort;
      }
      return true;
    } else {
      route.sort = 0;
      if (route.hidden !== undefined && route.hidden === true) {
        return true;
      } else {
        return false;
      }
    }
  } else {
    return true
  }
}

//根据路由名称获取菜单
function getMenu(name, menus) {
  for (let i = 0; i < menus.length; i++) {
    let menu = menus[i];
    if (name===menu.name) {
      return menu;
    }
  }
  return null;
}

//对菜单进行排序
function sortRouters(accessedRouters) {
  for (let i = 0; i < accessedRouters.length; i++) {
    let router = accessedRouters[i];
    if(router.children && router.children.length > 0){
      router.children.sort(compare("sort"));
    }
  }
  accessedRouters.sort(compare("sort"));
}

//降序比较函数
function compare(p){
  return function(m,n){
    let a = m[p];
    let b = n[p];
    return b - a;
  }
}

const permission = {
  state: {
    routers: {},
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = constantRouterMap.concat(routers);
    }
  },
  actions: {
    // 添加路由
    SetRoutes({ commit },username){
      return new Promise(resolve => {
        let currentRouter = [];
        if (username === 'admin') {
          currentRouter = systemRouterMap
        } else {
          currentRouter = doctorRouterMap
        }
        //对菜单进行排序
        sortRouters(currentRouter);
        commit('SET_ROUTERS', JSON.parse(JSON.stringify(currentRouter)));
        resolve();
      })
    },



    // GenerateRoutes({ commit }, username) {
    //   const accountID = Cookie.get('userID')
    //   return new Promise(resolve => {
    //
    //     getUserPermission(1).then(res => {
    //       let currentRouter = [];
    //       if (username === 'admin') {
    //         currentRouter = systemRouterMap
    //       } else {
    //         currentRouter = doctorRouterMap
    //       }
    //       const { menus } = res.data;
    //       const accessedRouters = currentRouter.filter(v => {
    //         if (hasPermission(menus, v)) {
    //           if (v.children && v.children.length > 0) {
    //             v.children = v.children.filter(child => {
    //               if (hasPermission(menus, child)) {
    //                 return child
    //               }
    //               return false;
    //             });
    //             return v
    //           } else {
    //             return v
    //           }
    //         }
    //         return false;
    //       });
    //       //对菜单进行排序
    //       sortRouters(accessedRouters);
    //       commit('SET_ROUTERS', accessedRouters);
    //       resolve();
    //     }).catch(e =>{
    //       tips('error','获取permission失败')
    //     });
    //   })// end getUserPermission
    // }
  }
};

export default permission;


